package modules

import (
	"crypto/md5"
	"fmt"
	"gosync/models"
	"io"
	"os"
)

//获取文件信息
func GetFileInfo(filename string) *models.SysFileInfo {
	fi, err := os.Lstat(filename)
	if err != nil {
		fmt.Println("info ERROR", err)
		return nil
	}
	fileHandle, err := os.Open(filename)
	if err != nil {
		fmt.Println("open ERROR", err)
		return nil
	}

	h := md5.New()
	_, err = io.Copy(h, fileHandle)
	fileInfo := &models.SysFileInfo{
		FName:  fi.Name(),
		FSize:  fi.Size(),
		FPerm:  fi.Mode().Perm(),
		FMtime: fi.ModTime(),
		FType:  fi.IsDir(),
		FMd5:   fmt.Sprintf("%x", h.Sum(nil)),
	}
	return fileInfo
}
